//
//  PSTreeLevel.m
//  PrefCore
//
//  Created by Sergey on 5/15/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import "PSTreeLevel.h"
#import "PSNode.h"

@interface PSTreeLevel ()

-(void)clearList:(PSLinkedList*)list except:(PSNode*)excluded;

@end

@implementation PSTreeLevel

-(instancetype)initWithRound:(uint32_t)round {
    if (self = [super init]) {
        _round = round;
        _pending = [[PSLinkedList alloc] init];
        _processed = [[PSLinkedList alloc] init];
        _complete = [[PSLinkedList alloc] init];
    }
    return self;
}

-(void)clearExcept:(PSNode*)excluded {
    [self clearList:_pending except:excluded];
    [self clearList:_processed except:excluded];
    [self clearList:_complete except:excluded];
}

-(void)clearList:(PSLinkedList*)list except:(PSNode*)excluded {
    [list start];
    while ([list hasNextEntry]) {
        PSNode *n = [list nextEntry];
        // Do not touch the excluded node
        if (n == excluded) {
            continue;
        }
        // Remove the node from the list
        [list removeCurrentEntry];
        // Detach children and put the position back to the pool
        [n destroy];
    }
}

@end
